package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import util.DBHelper;
import entity.Contents;
import entity.Execution;

public class ExecutionDAO {
	private Connection conn = null;
	private Statement st = null;

	public ExecutionDAO() {
		try {
			Class.forName("net.sourceforge.jtds.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433;DatabaseName=Contract", "sa",
					"123456");
			st = conn.createStatement();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public synchronized void release() {
		if (st != null) {
			try {
				st.close();
			} catch (Exception e) {
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e) {
			}
		}

	}

	public ArrayList<Execution> getAllExecution() {
		ArrayList<Execution> list = new ArrayList<Execution>();
		try {
			ResultSet rs = st.executeQuery("select * from execution;");
			while (rs.next()) {
				Execution ex = new Execution();
				ex.setCno(rs.getString("cno"));
				ex.setContext(rs.getString("context"));
				list.add(ex);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
		return list;
	}

	// 链接数据库，从数据库里：根据商品编号获得商品资料
	public Execution getExecutionByCno(String cno) {
		Connection conn;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			conn = DBHelper.getConnection();
			String sql = "select * from execution where cno=?;"; // SQL语句
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, cno);
			rs = stmt.executeQuery();
			if (rs.next()) {
				Execution ex = new Execution();
				ex.setCno(rs.getString("cno"));
				ex.setContext(rs.getString("context"));
				return ex;
			} else {
				return null;
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			// 释放数据集对象
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
			// 释放语句对象
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}

		}
	}

	// 获取最近浏览的前五条商品信息
	public ArrayList<Execution> getViewList(String list) {
		System.out.println("list:" + list);
		ArrayList<Execution> executionlist = new ArrayList<Execution>();
		int iCount = 5; // 每次返回前五条记录
		if (list != null && list.length() > 0) {
			String[] arr = list.split(",");
			System.out.println("arr.length=" + arr.length);
			if (arr.length >= 5) {// 如果商品记录大于等于5条
				// (i=8-1;i>=3;i--)
				for (int i = arr.length - 1; i >= arr.length - iCount; i--) {
					executionlist.add(getExecutionByCno(arr[i]));
				}
			} else {// 如果商品小于5条
				for (int i = arr.length - 1; i >= 0; i--) {
					executionlist.add(getExecutionByCno(arr[i]));
				}
			}
			return executionlist;
		} else {
			return null;
		}

	}

	public synchronized void execution_edit(String cno) throws Exception {
		try {
			Connection conn = null;
			PreparedStatement st = null;
			conn = DBHelper.getConnection();
			PreparedStatement stmt = null;
			ResultSet rs = null;
			String rs1 = "select * from execution where cno=?";
			stmt = conn.prepareStatement(rs1);
			stmt.setString(1, cno);
			rs = stmt.executeQuery();
			while (rs.next()) {
				if (rs.getString("context").equals("未执行")) {

					String sql = "update execution set context='执行中' where cno='" + cno + "'";
					st = conn.prepareStatement(sql);
					ResultSet rs2 = st.executeQuery();

				}

			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public synchronized void execution_finish(String cno) throws Exception {
		try {
			Connection conn = null;
			PreparedStatement st = null;
			PreparedStatement st1 = null;
			PreparedStatement stmt = null;
			ResultSet rs = null;
			conn = DBHelper.getConnection();
			String rs1 = "select * from execution where cno=?";
			stmt = conn.prepareStatement(rs1);
			stmt.setString(1, cno);
			rs = stmt.executeQuery();
			while (rs.next()) {
				if (rs.getString("context").equals("执行中")) {

					String sql = "update execution set context='已完成' where cno='" + cno + "'";
					st1 = conn.prepareStatement(sql);
					ResultSet rs2 = st1.executeQuery();
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();

		}
	}

}
